﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SP.Domain.Model;
using SP.Service;
using SP.User.WebApi.Filter;
using Swashbuckle.AspNetCore.Annotations;

namespace SP.User.WebApi.Controllers
{
    /// <summary>
    /// 角色关系模块
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class RelationController : ControllerBase
    {
        private readonly IRelationService _rela;
        public RelationController(IRelationService rela)
        {
            _rela = rela;
        }
        
        /// <summary>
        /// 用户角色关系添加
        /// </summary>
        /// <param name="Uid"></param>
        /// <param name="Rid"></param>
        /// <returns></returns>
        [HttpGet("AddRelation")]
        [Authorize]//授权特性，表示：下面的控制器中所有方法，都必须登陆之后才能访问
        [SwaggerOperation(Summary ="关系添加")]
        public IActionResult AddRelation(int Uid,string Rid)
        {
            if (Uid==null&&Rid==null)
            {
                throw new Exception("用户ID或角色id不能为空");
            }
            var result = _rela.AddRelation(Uid,Rid);
            if (result==null)
            {
                return Ok(new { code = 400, msg = "用户ID或角色id为空", data = result });
            }
            else
            {
                return Ok(new {code=200,msg="添加成功",data=result });
            }
        }
        /// <summary>
        /// 用户角色关系显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="UserName"></param>
        /// <returns></returns>
        [HttpGet("UserRoleRelation")]
        [SwaggerOperation(Summary = "用户角色关系显示")]
        [NoLog]//要去定义一个NoLog特性 ， 思考：如何才能定义一个自定义的特性呢？
        public IActionResult UserRoleRelation(int pageIndex,int pageSize,string? UserName)
        {
            //if (pageIndex<=0&&pageSize<=0)
            //{
            //    throw new Exception("当前页或页面大小不能小于0");   
            //}
            var result=_rela.RelationList(pageIndex,pageSize, UserName);
            if (result == null)
            {
                return Ok(new { code = 400, msg = "错误信息", data = result });
            }
            else
            {
                return Ok(new {code=200,msg="显示成功",data=result });
            }
        }
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("RoleList")]
        [ResponseHeader("Date", "yyy")]
        
        public IActionResult RoleList()
        {
            var result = _rela.RoleList();
            if (result == null)
            {
                return Ok(new {code=400,msg="错误信息",data=result });
            }
            else
            {
                return Ok(new {code=200,msg="角色列表显示成功",data=result });
            }
        }
        [HttpPost("DeleteRelation")]
        public IActionResult DeleteRelation(RoleUserRelation model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            var result = _rela.DeleteRelation(model);
            if (result==null)
            {
                return Ok(new {code=400,msg="删除信息不能为空",data=result });
            }
            else
            {
                return Ok(new { code=200,msg="删除成功",data=result});
            }
        }
        /// <summary>
        /// 权限列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("PermissionList")]
        public IActionResult PermissionList()
        {
            var result = _rela.PermissList();
            if (result==null)
            {
                return Ok(new {code=400,msg="异常错误",data=result });
            }
            else
            {
                return Ok(new {code=200,msg="权限列表显示成功",data=result });
            }
        }
        /// <summary>
        /// 通过用户id获取菜单
        /// </summary>
        /// <returns></returns>
        //[HttpGet]
        //public IActionResult GetMenus(int userId)
        //{
        //    //第一个，先去redis中获取该用户的菜单信息
        //    //如果没有该用户的菜单，则去数据库中获取
        //    //这里的key是角色的id
        //    var key = $"UserId_{userId}_menu";
        //    //查询redis 是否存入信息
        //}
        /// <summary>
        /// 存储过程用户列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="UserName"></param>
        /// <returns></returns>
        [HttpGet("Userpagelist")]
        public IActionResult Userpagelist(int pageIndex, int pageSize, string? UserName)
        {
            var result=_rela.UserPageList(pageIndex, pageSize, UserName);
            if (result == null)
            {
                return Ok(new {code=400,msg="异常错误",data= result });
            }
            else
            {
                return Ok(new { code = 200, msg = "用户列表显示成功", data = result });
            }
        }
        [HttpGet("NavPermissList")]
        public IActionResult NavPermissList()
        {
            var result = _rela.NavPermissList();
            if (result == null)
            {
                return Ok(new { code = 400, msg = "异常错误", data = result });
            }
            else
            {
                return Ok(new { code = 200, msg = "用户列表显示成功", data = result });
            }
        }
    }
}
